<?php

namespace app\admin\controller;

use think\Exception;

class UserController extends CommonController
{
    protected $initModel            =   true; // 允许初始化model
    protected $is_strictly          =   true; // 允许进行严格的验证
    protected $is_verify            =   false;


    protected function _beforeParam(&$param)
    {
        $param['slot']              =empty($param['slot'])?randString():$param['slot'];
        $param['password']          =md5($param['password'].$param['slot']);
        $param                      =array_map(function($element){
            return                  is_array($element)?implode($element,','):$element;
        },$param);
    }

    protected function _beforeWhere(&$where)
    {
        unset($where['page']);
        if($this->action==='update' && $this->request->isPut()){
            $exists                     =   array_key_exists('id',$where);
            if(false===$exists){throw new Exception('抱歉参数错误',self::RESPONSE_SUCCESS);}
            $where_param                =   [];
            $where_param['id']          =   $where['id'];
            $where                      =   $where_param;
        }
    }
    protected function _beforeList(&$list)
    {

        $list['data']=array_map(function(&$element){
            if(true==array_key_exists('hobby',$element) && $element['hobby']!=''){
                $element['hobby']           =explode(',',$element['hobby']);
            };
            return $element;
        },$list['data']);
    }


    protected function _endParam(&$where)
    {

        $where['password']=!empty($where['password'])?'':'';
    }

}
